package com.ys.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ys.gulimall.product.entity.BrandEntity;
import com.ys.gulimall.product.vo.BrandVo;
import org.springframework.web.bind.annotation.*;

import com.ys.gulimall.product.entity.CategoryBrandRelationEntity;
import com.ys.gulimall.product.service.CategoryBrandRelationService;
import com.ys.common.utils.PageUtils;
import com.ys.common.utils.R;

import javax.annotation.Resource;


/**
 * 品牌分类关联
 *
 * @author yeswater
 * @email ruobairuo@gmail.com
 * @date 2021-07-16 17:43:10
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;
	
	/**
	 * /product/categorybrandrelation/brands/list
	 * 1、Controller：处理请求，接收和效验数据
	 * 2、Service接收Controller传来的数据，进行业务处理
	 * 3、Controller接收Service处理完的数据，封装页面指定的vo
	 */
	@GetMapping(value = "/brands/list")
	public R relationBrandsList(@RequestParam(value = "catId",required = true) Long catId) {
		
		List<BrandEntity> vos = categoryBrandRelationService.getBrandsByCatId(catId);
		
		List<BrandVo> collect = vos.stream().map(item -> {
			BrandVo brandVo = new BrandVo();
			brandVo.setBrandId(item.getBrandId());
			brandVo.setBrandName(item.getName());
			return brandVo;
		}).collect(Collectors.toList());
		
		return R.ok().put("data",collect);
	}
	
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	//@RequiresPermissions("product:categorybrandrelation:list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = categoryBrandRelationService.queryPage(params);
		
		return R.ok().put("page", page);
	}

	/**
	 * 获取当前品牌关联的所有分类列表列表
	 */
	@GetMapping(value = "/catelog/list")
	//@RequiresPermissions("product:categorybrandrelation:list")
	public R catelogList(@RequestParam Map<String, Object> params,@RequestParam("brandId") Long brandId){
		
		List<CategoryBrandRelationEntity> data = categoryBrandRelationService.
				                                         list(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
		
		return R.ok().put("data", data);
	}


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
        public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
        public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
        public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
        public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
